class QuestionField
  include Mongoid::Document

  embedded_in :question_type

  field :position, type: Integer
  field :name, type: String
  field :max_len, type: Integer
  field :min_len, type: Integer
  field :hidden, type: Boolean, default: false

  scope :input, where(hidden: false)

  def validate_field(data, error_texts)
    if self.max_len < 0
      return
    end
    if data.length > self.max_len
      error_texts[self.name] = :too_long
    elsif data.length < self.min_len
      error_texts[self.name] = :too_short
    end
  end
end
